﻿/**
 * Created by Administrator on 2017/8/7.
 */
define(['api','VisitRecordInfo','AddVisitRecord'],function (api, VisitRecordInfo, AddVisitRecord) {
    var VisitRecord = {
        name: 'visit-record',
        template: ''
        + '<div class="sellBox" v-bind:class="classBtnIcon">'
        + '<div class="sellVisit">'
        + '    <ul>'
        + '         <visit-record-info v-for="item in visitList" v-bind:visitRecord="item"></visit-record-info>'
        + '    </ul>'
        + '</div>'
        + '<div class="sellVisitBtn" v-if="poolType===\'StaffPool\'">'
        + '    <div class="bg-blue-btn">'
        + '        <Button type="primary" v-on:click="showShiftCustomerToOtherStaff">转移客户</Button>'
        + '    </div>'
        + '    <div class="bg-blue-btn">'
        + '        <Button type="primary" v-on:click="showAddVisitRecord" style="margin-left: 105px">'
        + '            填写最新回访记录'
        + '        </Button>'
        + '        <add-visit-record></add-visit-record>'
        + '    </div>'
        + '    <div class="bg-white-btn">'
        + '        <Button type="primary" v-on:click="showShiftCustomerToDept">放入公客池</Button>'
        + '    </div>'
        + '</div>'
        + '<div class="sellVisitBtn" v-if="poolType===\'DeptPool\' || poolType===\'CompanyPool\'">'
        + '    <div class="bg-blue-btn">'
        + '        <Button type="primary" v-on:click="showShiftCustomerToStaff" style="width:150px;">转为个人客户</Button>'
        + '    </div>'
        + '    <div class="bg-blue-btn">'
        + '        <Button type="primary" v-on:click="showShiftCustomerToOtherDept">转移客户归属</Button>'
        + '    </div>'
        + '    <div class="bg-blue-btn">'
        + '        <Button type="primary" v-on:click="showAddVisitRecord">'
        + '            填写最新回访记录'
        + '        </Button>'
        + '        <add-visit-record></add-visit-record>'
        + '    </div>'
        + '    <div class="bg-white-btn">'
        + '        <Button type="primary" v-on:click="showStopTrack">断开跟踪</Button>'
        + '    </div>'
        + '</div>'
        + '<div class="sellVisitBtn" v-if="poolType===\'BigCustomer\' || poolType===\'TradedCustomer\'|| poolType===\'Customer\'">'
        + '    <div class="bg-blue-btn" style="padding-left: 40%;">'
        + '        <Button type="primary" v-on:click="showAddVisitRecord"  style="width:150px;">'
        + '            填写最新回访记录'
        + '        </Button>'
        + '        <add-visit-record></add-visit-record>'
        + '    </div>'
        + '</div>'
        + '</div>',
        props: {
            poolType: String
        },
        data: function () {
            return {
                visitList: [],
                customerId: '',
                nodeId: "",
                tradedNodeId: "",
                classBtnIcon: {
                    "my-sell-btn": this.poolType === 'StaffPool',
                    "sec-sell-btn": this.poolType === 'DeptPool',
                    "com-sell-btn": this.poolType === 'CompanyPool',
                    "big-sell-btn": this.poolType === 'BigCustomer',
                }
            }
        },
        components: {
            VisitRecordInfo: VisitRecordInfo,
            AddVisitRecord: AddVisitRecord,
        },
        mounted: function () {
            var thisVisit = this;
            //监听getCustomerDetail
            window.bus.$on("getCustomerDetail", function (Id) {
                thisVisit.customerId = Id;
                thisVisit.getVisitInfo(Id);
            });
            //监视submitVisitRecordSuccess
            window.bus.$on("submitVisitRecordSuccess", function () {
                thisVisit.getVisitInfo(thisVisit.customerId);
            });
            //监视stopTrackSuccess
            window.bus.$on("stopTrackSuccess", function () {
                thisVisit.getVisitInfo(thisVisit.customerId);
            });
        },
        methods: {
            getVisitInfo: function (Id) {
                var thisVisit = this;
                api.Get_Visit_Info({customerId: Id})
                    .done(function (data) {
                        if (data.IsSuccess) {
                            thisVisit.visitList = data.ResultObject;
                            if (thisVisit.visitList.length > 0) {
                                thisVisit.nodeId = thisVisit.visitList[thisVisit.visitList.length - 1].NodeId;
                                thisVisit.tradedNodeId = thisVisit.visitList[thisVisit.visitList.length - 1].TradedNodeId;
                            }
                            else
                            {
                                api.Get_Customer_Info({customerId: Id})
                                    .done(function (data) {
                                        if (data.IsSuccess) {
                                            thisVisit.nodeId = data.ResultObject.NodeId;
                                            thisVisit.tradedNodeId = data.ResultObject.TradedNodeId;
                                        } else {
                                            thisVisit.$Message.error(data.ErrMsg);
                                        }
                                    })
                                    .fail(function () {
                                        thisVisit.$Message.error('查询出错');
                                    });
                            }
                        } else {
                            thisVisit.$Message.error(data.ErrMsg);
                        }
                    })
                    .fail(function () {
                        thisVisit.$Message.error('查询出错');
                    });
            },
            showShiftCustomerToDept() {
                //触发showShiftCustomerToDept
                window.bus.$emit("showShiftCustomerToDept", [{ Id: this.customerId, NodeId: this.nodeId}]);
            },
            showShiftCustomerToOtherStaff() {
                //触发showShiftCustomerToOtherStaff
                window.bus.$emit("showShiftCustomerToOtherStaff", { customerIdList: [this.customerId], oldStaffId: this.nodeId});
            },

            showShiftCustomerToStaff() {
                //触发showShiftCustomerToStaff
                window.bus.$emit("showShiftCustomerToStaff", [this.customerId]);
            },
            showShiftCustomerToOtherDept() {
                //触发showShiftCustomerToOtherDept
                window.bus.$emit("showShiftCustomerToOtherDept", { customerIdList: [{ customerId: this.customerId, tradedNodeId: this.tradedNodeId }], oldNodeId: this.nodeId });
            },
            showAddVisitRecord() {
                //触发showSubmitVisitRecord
                window.bus.$emit("showSubmitVisitRecord", this.customerId);
            },
            showStopTrack() {
                //触发showStopTrack
                window.bus.$emit("showStopTrack", this.customerId);
            },

        },
    };
    return VisitRecord;
});

